/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mx.its.dsw2.biblioteca.bl;

import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import mx.its.dsw2.biblioteca.Detalleprestamo;
import mx.its.dsw2.biblioteca.Libro;
import mx.its.dsw2.biblioteca.Prestamo;
import mx.its.dsw2.biblioteca.Usuario;
import mx.its.dsw2.biblioteca.dao.DetalleprestamoDaoLocal;
import mx.its.dsw2.biblioteca.dao.PrestamoDaoLocal;
import mx.its.dsw2.biblioteca.enumeracion.EstadoDetallePrestamo;
import mx.its.dsw2.biblioteca.enumeracion.EstadoTransaccion;

/**
 *
 * @author chava
 */
@Stateless
public class PrestamoBl implements PrestamoBlLocal {

    @EJB
    private DetalleprestamoDaoLocal detalleprestamoDao;
    @EJB
    private PrestamoDaoLocal prestamoDao;

    @Override
    public EstadoTransaccion register(Prestamo lend, List<Libro> book) {
        int avalible = 3;
        List<Prestamo> incopleteLends = prestamoDao.findIncomplete(lend.getIdUsuario());
        for (Prestamo p : incopleteLends) {
            avalible = avalible - detalleprestamoDao.findLends(p).size();
        }
        if (avalible <= 0 || book.size() > avalible) {
            return EstadoTransaccion.INCOMPLETO;
        } else {
            prestamoDao.create(lend);
            for (Libro b : book) {
                Detalleprestamo dp = new Detalleprestamo();
                dp.setIdLibro(b);
                dp.setIdPrestamo(lend);
                dp.setEstado(EstadoDetallePrestamo.PRESTADO.ordinal());
                detalleprestamoDao.create(dp);
            }
            return EstadoTransaccion.COMPLETO;
        }

    }

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
    @Override
    public EstadoTransaccion takeBack(List<Libro> books) {
        for (Libro book : books) {
            Detalleprestamo dp = detalleprestamoDao.findBooks(book);
            if (dp == null) {
                return EstadoTransaccion.INCOMPLETO;
            } else {
                dp.setEstado(EstadoDetallePrestamo.REGRESADO.ordinal());
                detalleprestamoDao.edit(dp);
            }
        }
        return null;
    }

    @Override
    public List<Prestamo> incopleteLendsUser(Usuario user) {
        return prestamoDao.findIncomplete(user);
    }

    @Override
    public List<Libro> lendBooksByUser(Usuario user) {
        List<Prestamo> incopleteLends = prestamoDao.findIncomplete(user);
        if (!incopleteLends.isEmpty()) {
            List<Libro> listBook = null;
            for (Prestamo lend : incopleteLends) {
                List<Detalleprestamo> dp = lend.getDetalleprestamoList();
                for (Detalleprestamo d : dp) {
                    listBook.add(d.getIdLibro());
                }
            }
            return listBook;
        } else {
            return null;
        }

    }

    @Override
    public List<Prestamo> generalIncopletelends() {
        return prestamoDao.findIncompleteLends();
    }

    @Override
    public List<Libro> generalLendsBooks() {
          List<Prestamo> incopleteLends = prestamoDao.findIncompleteLends();
        if (!incopleteLends.isEmpty()) {
            List<Libro> listBook = null;
            for (Prestamo lend : incopleteLends) {
                List<Detalleprestamo> dp = lend.getDetalleprestamoList();
                for (Detalleprestamo d : dp) {
                    listBook.add(d.getIdLibro());
                }
            }
            return listBook;
        } else {
            return null;
        }
    }
    
}
